package com.neu.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import java.sql.Statement;
import com.neu.ceshi.Demo01;

public class JDBCDemo01 {
	private static String url = "jdbc:mysql://localhost:3306/mjq";
	private static String userName = "root";
	private static String pass = "mysql";
	private static String driver = "com.mysql.jdbc.Driver";
	public static Connection conn;
	public static Statement st;
	//  写一个静态代码块
	static {
		try {
		//  驱动的加载   对应的方法是    Class.forName(driver);
			//  先生成下面的代码   然后快捷生成  try{}
			Class.forName(driver);
			//  生成    Connection 对象
			conn=DriverManager.getConnection(url,userName,pass);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	
	
	/**
	 *   定义增加操作的方法( 增加 tb_user 表中的数据)
	 * @throws SQLException 
	 */
	public static boolean insert(Demo01 d) throws SQLException {
		boolean flag = false;
		String sql ="insert into mjq.tb_user value("+null+",'"+d.getCuername()+"','"
				+d.getSddress()+"','"+d.getEmile()+"')";
		st = conn.createStatement();
		int a =st.executeUpdate(sql);
		if(a>0) {
			flag = true;
		}
		return flag;
		
	}
	
	
	
	
	/**
	 *   定义删除操作的方法( 删除 tb_user 表中的数据)
	 * @throws SQLException 
	 */
	public static boolean delete(int id) throws SQLException {
		boolean flag = false;
		String sql = "delete from tb_user where id="+id;
		st = conn.createStatement();
		int a =st.executeUpdate(sql);
		if(a>0) {
			flag = true;
		}
		return flag;
		
	}
	public static void main(String[] args) throws SQLException {
//		boolean b = insert(new Demo01(0,"你叫什么","中国","www.xixi.com"));
//		if(b = true) {
//			System.out.println("录入成功");
//		}else {
//			System.out.println("录入失败");
//		}
	
	boolean flag = delete(5);
	if(flag = true) {
		System.out.println("成功");
	}else {
		System.out.println("失败");
	}
}
	
}
